<template>
  <PageDialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <PageFilter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.list"
    />
    <!-- 条件栏 -->
    <DialogTable
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="tableInfo.api"
      v-model:query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlightRow="highlightRow"
      :check-box="checkBox"
      :pager="false"
      @handleEvent="handleEvent"
    />
  </PageDialog>
</template>
<script setup name="AddPurchOrder">
import { getPendDeliverOrderPage } from '@/api/supplier.js'

const { proxy } = getCurrentInstance()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: false,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
})
const shows = computed({
  get() {
    return props.showDialog
  },
  set(val) {
    proxy.$emit('update:showDialog', val) // 更新父组件shows
  },
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    orderNoLike: '',
    supplierAuth: 'supplierId',
  },
  list: [
    {
      type: 'input',
      label: '销售单',
      value: 'orderNoLike',
      clearable: true,
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: getPendDeliverOrderPage,
  fieldList: [
    { label: '销售单号', value: 'orderNo' },
    { label: '创建日期', value: 'createTime' },
    { label: '状态', value: 'statusValue' },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: '销售单导入',
  width: '50%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: true,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')

function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function save() {
  if (selectRow.value) {
    handleClose()
    proxy.$emit('callback', selectRow.value)
  } else {
    proxy.$message({
      type: 'warning',
      duration: 3500,
      showClose: true,
      message: '请选择销售单',
    })
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      dialogInfo.value.btList[1].disabled = !selectRow.value
      break
    case 'tableCheck':
      if (data.length) {
        selectRow.value = data
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
  }
}
function handleClose() {
  shows.value = false
}
onMounted(() => {
  proxy.$nextTick(() => {
    getList()
  })
})
</script>
